<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of user model
 *
 * @author Nazmul
 */
class Usermodel extends CI_Model {

    // tabel
    var $userTable = 'users';
    // tabel field of user
    var $pkid = 'pkid';
    var $userName = 'username';
    var $password = 'password';
    var $name = 'name';
    var $email = 'email';
    var $status = 'status';

    function __construct() {
        parent::__construct();
    }

    // check userid(username) is exists or not
    function isUsernameExists($userid) {
        try {
            $this->db->where($this->userName, $userid);
            $query = $this->db->get($this->userTable);
            return $query->num_rows() ? true : false;
        } catch (Exception $exc) {
            error_log("Usermodel >> isUserIdExists :: line 29");
        }
    }

    // check email address is already entry or not
    function isEmailExists($email) {
        try {
            $this->db->where($this->email, $email);
            $query = $this->db->get($this->userTable);
            return $query->num_rows() ? true : false;
        } catch (Exception $exc) {
            error_log("Usermodel >> isEmailExists :: line 40");
        }
    }

    // user login with userid(username)/email and password
    public function getUserInfo($userid) {
        $result = array();
        try {
            $this->db->where($this->pkid, $userid);
            $this->db->where($this->status, 'active');
            $query = $this->db->get($this->userTable);
            if ($query->num_rows()) {
                $result = $query->row_array(0);
            }
        } catch (Exception $exc) {
            error_log("Usermodel >> userLogin :: line 51");
        }
        return $result;
    }
    
    // user login with userid(username)/email and password
    public function userLogin($email, $password) {
        $userid = 0;
        try {
            $this->db->where($this->status, 'active');
            $this->db->where($this->email, $email);
            $this->db->where($this->password, $password);
            $query = $this->db->get($this->userTable);
            $query->num_rows();
            if ($query->num_rows()) {
                $result = $query->row_array(0);
                $userid = $result['pkid'];
            }
        } catch (Exception $exc) {
            error_log("Usermodel >> userLogin :: line 51");
        }
        return $userid;
    }

    // user forgot password check userid valid or not and set expair datetime
    function userForgotPassword($email, $userid) {
        try {
            $today = getdate(); // get date time from builtin system
            $fiveDay = (24 * 60 * 60 * 5); // calculate 5 days number
            $setDateTime = $today[0] + $fiveDay; // set expire date time 
            // check user or email is alive or not
            if (!empty($email) || !empty($userid)) {
                if (!empty($email)) {
                    $this->db->where($this->email, $email);
                } else if (!empty($userid)) {
                    $this->db->where($this->userName, $userid);
                }
                $this->db->where($this->status, 'active');
                $query = $this->db->get($this->userTable);
                if ($query->num_rows()) {
                    // set date and time by email or userid
                    if (!empty($email)) {
                        $this->db->where($this->email, $email);
                    } else if (!empty($userid)) {
                        $this->db->where($this->userName, $userid);
                    }
                    $this->db->update($this->userTable, array($this->forgotPasswordExpireDateTime => $setDateTime));
                    return $setDateTime;
                }
            }
            return 0;
        } catch (Exception $exc) {
            error_log("Usermodel >> userForgotPassword :: line 72");
        }
    }

    // forgot password expire date time in int
    function getUserForgotExpireDT(array $userdata) {
        try {
            $this->db->where($this->userName, trim($userdata['username']));
            $this->db->where($this->primaryEmail, trim($userdata['email']));
            $query = $this->db->get($this->userTable);
            if ($query->num_rows()) {
                return $query->row_array(0);
            } else {
                return array();
            }
        } catch (Exception $exc) {
            error_log("Usermodel >> userForgotPassword :: line 72");
        }
    }

    // Change Password
    function updatePassword($newPass, $currPass) {
        try {
            if ($this->retailpassageuserauth->pkId()) {
                $this->db->where($this->pkid, $this->retailpassageuserauth->pkId());
                $this->db->where($this->password, $currPass);
                $this->db->from($this->userTable);
                $_query = $this->db->get();
                if ($_query->num_rows() > 0) {
                    $updateingData = array($this->password => $newPass);
                    $this->db->where($this->pkid, $this->retailpassageuserauth->pkId());
                    $this->db->update($this->userTable, $updateingData);
                    return true;
                }
            }
            return false;
        } catch (Exception $exc) {
            //error_log($exc->getTraceAsString());
        }
    }

    // Change BizOwner Password
    function updatePasswordByUserInfo($newPass, $userdata) {
        try {
            // password and expire date time update
            $updateingData = array($this->password => $newPass, $this->forgotPasswordExpireDateTime => 0);
            $this->db->where($this->userId, trim($userdata['userid']));
            $this->db->where($this->primaryEmail, trim($userdata['email']));
            $this->db->update($this->userTable, $updateingData);
            return true;
        } catch (Exception $exc) {
            //error_log($exc->getTraceAsString());
        }
    }

}


